from rest_framework import serializers
from goods.models import SPU,Brand,GoodsCategory



class SPUSerializer(serializers.ModelSerializer):
    # 品牌
    brand_id = serializers.IntegerField()
    brand = serializers.StringRelatedField(read_only=True)

    # 只进行名称返回
    category1 = serializers.StringRelatedField(read_only=True)
    category2 = serializers.StringRelatedField(read_only=True)
    category3 = serializers.StringRelatedField(read_only=True)

    # 将字段转换成前端需要的格式
    category1_id = serializers.IntegerField()
    category2_id = serializers.IntegerField()
    category3_id = serializers.IntegerField()


    class Meta:
        #指定根据哪个模型类生成序列化字段
        model=SPU
        fields='__all__'


class BrandSerializer(serializers.ModelSerializer):
    """
        品牌序列化器
    """

    class Meta:
        model = Brand
        fields = '__all__'


class GoodsCategorySerializer(serializers.ModelSerializer):
    """
        分类序列化器
    """

    class Meta:
        model = GoodsCategory
        fields = '__all__'
